package org.budo.validation.exception;

import java.lang.annotation.Annotation;

import org.budo.support.lang.util.StringUtil;

/**
 * @author lmw
 */
public abstract class AbstractBudoValidationException extends RuntimeException {
    private static final long serialVersionUID = 1099507732561430240L;

    private Integer parameterIndex;

    private String parameterName;

    private Object parameterValue;

    private Annotation annotation;

    public AbstractBudoValidationException() {}

    public AbstractBudoValidationException(Integer parameterIndex, String parameterName, Annotation annotation, Object parameterValue, String message) {
        super(message);

        this.setParameterIndex(parameterIndex);
        this.setParameterName(parameterName);
        this.setParameterValue(parameterValue);
        this.setAnnotation(annotation);
    }

    public Annotation getAnnotation() {
        return annotation;
    }

    public void setAnnotation(Annotation annotation) {
        this.annotation = annotation;
    }

    public Object getParameterValue() {
        return parameterValue;
    }

    public void setParameterValue(Object parameterValue) {
        this.parameterValue = parameterValue;
    }

    public Integer getParameterIndex() {
        return parameterIndex;
    }

    public String getParameterName() {
        return parameterName;
    }

    public void setParameterIndex(Integer parameterIndex) {
        this.parameterIndex = parameterIndex;
    }

    public void setParameterName(String parameterName) {
        this.parameterName = parameterName;
    }

    public String getMessage() {
        if (!StringUtil.isEmpty(super.getMessage())) {
            return super.getMessage();
        }

        return this.buildMessage();
    }

    public Throwable fillInStackTrace() {
        return this;
    }

    protected abstract String buildMessage();

    /**
     * 如果两个值相等就返回空，否则返回第一个值
     */
    protected static String validationMessage(String message, String messageDefault) {
        if (null != message && null != messageDefault && message.equals(messageDefault)) {
            return null;
        }

        return message;
    }
}
